<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 100vh;
            background: linear-gradient(to right, #66a6ff, #89f7fe);
            padding: 0 10px;
        }

        .register-container {
            background: #fff;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            width: 320px;
            margin-bottom: 20px;
        }

        .register-container h2 {
            margin-bottom: 30px;
            text-align: center;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
        }

        .form-group button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-size: 16px;
        }

        .form-group button:hover {
            background-color: #0056b3;
        }

        /* 警示框样式 */
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            display: none; /* 默认隐藏 */
            opacity: 0; /* 初始透明 */
            transition: opacity 0.5s ease; /* 动画效果 */
            width: 300px; /* 设置警示框宽度 */
            position: relative;
        }

        .alert.show {
            display: block; /* 显示警示框 */
            opacity: 1; /* 设置透明度 */
        }

        .alert.success {
            background-color: #d4edda; /* 成功背景色 */
            color: #155724; /* 成功文字色 */
            border: 1px solid #c3e6cb; /* 成功边框色 */
        }

        .alert.error {
            background-color: #f8d7da; /* 错误背景色 */
            color: #721c24; /* 错误文字色 */
            border: 1px solid #f5c6cb; /* 错误边框色 */
        }

        .alert .closebtn {
            position: absolute;
            top: 0;
            right: 10px;
            font-size: 20px;
            cursor: pointer;
        }
        
        a {
            color: #007bff;
            text-decoration: none;
            display: block;
            text-align: center;
            margin-top: 10px;
        }
        
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div id="alert" class="alert">
    <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>
    <span id="alertMessage"></span>
</div>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>
        <div class="form-group">
            <input type="text" id="name" name="name" placeholder="请输入真实姓名" required>
        </div>
        <div class="form-group">
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>
        <div class="form-group">
            <input type="password" id="confirmPassword" name="confirmPassword" placeholder="请再次输入密码" required>
        </div>
        <div class="form-group">
            <input type="email" id="email" name="email" placeholder="请输入电子邮箱" required>
        </div>
        <div class="form-group">
            <button type="submit">注册</button>
        </div>
        <a href="/user/login">已有账号？点击登录</a>
    </form>
</div>

<script>
    document.getElementById('registerForm').addEventListener('submit', function (event) {
        event.preventDefault(); // 防止默认提交行为
        
        const username = document.getElementById('username').value;
        const name = document.getElementById('name').value;
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const email = document.getElementById('email').value;
        
        const alertDiv = document.getElementById('alert');
        const alertMessage = document.getElementById('alertMessage');

        alertDiv.style.display = 'none'; // 隐藏警示框以准备显示新的消息
        alertDiv.classList.remove('show', 'success', 'error'); // 清除所有状态
        
        // 验证两次密码是否一致
        if (password !== confirmPassword) {
            alertMessage.textContent = '两次密码输入不一致';
            alertDiv.classList.add('error');
            alertDiv.classList.add('show');
            alertDiv.style.display = 'block';
            return;
        }
        
        // 准备要发送的数据
        const userData = {
            username: username,
            name: name,
            password: password,
            email: email
        };

        // 发送注册请求
        fetch('/users/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(userData)
        })
        .then(response => response.json())
        .then(data => {
            if (data.status === "200") {
                alertMessage.textContent = '注册成功！即将跳转到登录页面...';
                alertDiv.classList.add('success');
                alertDiv.classList.add('show');
                alertDiv.style.display = 'block';
                
                // 设置延时再跳转
                setTimeout(() => {
                    window.location.href = '/user/login'; // 修正跳转到登录页面的路径
                }, 2000); // 2秒后跳转
            } else {
                alertMessage.textContent = '注册失败: ' + data.message;
                alertDiv.classList.add('error');
                alertDiv.classList.add('show');
                alertDiv.style.display = 'block';
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alertMessage.textContent = '请求失败，请稍后重试';
            alertDiv.classList.add('error');
            alertDiv.classList.add('show');
            alertDiv.style.display = 'block';
        });
    });
</script>
</body>
</html> 